package impl

import (
	"context"
	pb "gitee.com/micro-services/protocol/area/server"
	compb "gitee.com/micro-services/protocol/common"
	"micro.area.server/do"
	"micro.area.server/models"
	"time"
)

func AreaList(ctx context.Context) ([]*pb.AreaList, error) {
	areas, err := do.AreaList(ctx)
	if err != nil {
		return nil, err
	}

	areaList := make([]*pb.AreaList, 0)
	for _, v := range areas {
		areaList = append(areaList, &pb.AreaList{
			ID:       v.ID,
			ParentID: v.ParentID,
			Level:    v.Level,
			Name:     v.Name,
		})
	}

	return areaList, nil
}

func CreateCircle(ctx context.Context, r *pb.CreateCircleReq) (*pb.CreateCircleRsp, error) {
	circle := &models.VsBusinessCircle{
		CircleName:      r.CircleName,
		CircleImage:     r.CircleImage,
		CircleBigImage:  r.CircleBigImage,
		CircleLongitude: r.CircleLongitude,
		CircleLatitude:  r.CircleLatitude,
		AreaId:          r.AreaID,
		DetailAddress:   r.DetailAddress,
		IsHot:           r.IsHot,
		CreatedTime:     time.Now().Unix(),
		UpdatedTime:     time.Now().Unix(),
	}

	err := do.CreateCircle(circle)
	if err != nil {
		return &pb.CreateCircleRsp{
			RetCode: 40001,
			Msg:     "create circle error",
		}, err
	}

	return &pb.CreateCircleRsp{
		RetCode: 0,
		Msg:     "success",
	}, nil
}

func DeleteCircle(ctx context.Context, r *pb.DeleteCircleReq) (*pb.DeleteCircleRsp, error) {
	circleIDs := make([]interface{}, 0)
	for _, v := range r.BusinessCircleID {
		circleIDs = append(circleIDs, v)
	}

	err := do.DeleteCircle(circleIDs)
	if err != nil {
		return &pb.DeleteCircleRsp{
			RetCode: 40001,
			Msg:     "delete circle error",
		}, err
	}

	return &pb.DeleteCircleRsp{
		RetCode: 0,
		Msg:     "success",
	}, nil
}

func BatchCircleInfo(ctx context.Context, r *pb.BatchCircleInfoReq) ([]*compb.BusinessCircleInfo, error) {
	circleIDs := make([]interface{}, 0)
	for _, v := range r.BusinessCircleID {
		circleIDs = append(circleIDs, v)
	}

	circles, err := do.CircleInfo(circleIDs)
	if err != nil {
		return nil, err
	}

	areaMap, err := do.AreaListMap(ctx)
	if err != nil {
		return nil, err
	}

	circleInfo := make([]*compb.BusinessCircleInfo, 0)
	for _, v := range circles {
		circleInfo = append(circleInfo, &compb.BusinessCircleInfo{
			BusinessCircleID:   v.Id,
			BusinessCircleName: v.CircleName,
			CircleImage:        v.CircleImage,
			CircleBigImage:     v.CircleBigImage,
			CircleLongitude:    v.CircleLongitude,
			CircleLatitude:     v.CircleLatitude,
			AreaID:             v.AreaId,
			AreaName:           areaMap[v.AreaId].Name,
			DetailAddress:      v.DetailAddress,
			IsHot:              v.IsHot,
		})
	}

	return circleInfo, nil
}

func SearchCircle(ctx context.Context, r *pb.SearchCircleReq) ([]*compb.BusinessCircleInfo, int64, error) {
	areaMap, err := do.AreaListMap(ctx)
	if err != nil {
		return nil, 0, err
	}

	list, count, err := do.SearchCircle(r.AreaID, r.IsHot, r.Limit, r.Offset, r.CircleName)
	if err != nil {
		return nil, 0, nil
	}

	circleInfo := make([]*compb.BusinessCircleInfo, 0)
	for _, v := range list {
		circleInfo = append(circleInfo, &compb.BusinessCircleInfo{
			BusinessCircleID:   v.Id,
			BusinessCircleName: v.CircleName,
			CircleImage:        v.CircleImage,
			CircleBigImage:     v.CircleBigImage,
			CircleLongitude:    v.CircleLongitude,
			CircleLatitude:     v.CircleLatitude,
			AreaID:             v.AreaId,
			AreaName:           areaMap[v.AreaId].Name,
			DetailAddress:      v.DetailAddress,
			IsHot:              v.IsHot,
		})
	}

	return circleInfo, count, nil
}
